{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4e28d433",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# Padding and Stride\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "49dab7e8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:43:14.385177Z",
     "iopub.status.busy": "2023-08-18T19:43:14.384720Z",
     "iopub.status.idle": "2023-08-18T19:43:16.173429Z",
     "shell.execute_reply": "2023-08-18T19:43:16.172321Z"
    },
    "origin_pos": 3,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9af6d7d7",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Apply 1 pixel of padding on all sides"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b5e8917d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:43:16.177580Z",
     "iopub.status.busy": "2023-08-18T19:43:16.176879Z",
     "iopub.status.idle": "2023-08-18T19:43:16.211812Z",
     "shell.execute_reply": "2023-08-18T19:43:16.210995Z"
    },
    "origin_pos": 8,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([8, 8])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def comp_conv2d(conv2d, X):\n",
    "    X = X.reshape((1, 1) + X.shape)\n",
    "    Y = conv2d(X)\n",
    "    return Y.reshape(Y.shape[2:])\n",
    "\n",
    "conv2d = nn.LazyConv2d(1, kernel_size=3, padding=1)\n",
    "X = torch.rand(size=(8, 8))\n",
    "comp_conv2d(conv2d, X).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fe1e688",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Setting different padding numbers for height and width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1aa91aee",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:43:16.215455Z",
     "iopub.status.busy": "2023-08-18T19:43:16.214828Z",
     "iopub.status.idle": "2023-08-18T19:43:16.221907Z",
     "shell.execute_reply": "2023-08-18T19:43:16.221110Z"
    },
    "origin_pos": 13,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([8, 8])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conv2d = nn.LazyConv2d(1, kernel_size=(5, 3), padding=(2, 1))\n",
    "comp_conv2d(conv2d, X).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b0af68b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Set the strides on both the height and width to 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cc9ed33d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:43:16.225546Z",
     "iopub.status.busy": "2023-08-18T19:43:16.225010Z",
     "iopub.status.idle": "2023-08-18T19:43:16.232355Z",
     "shell.execute_reply": "2023-08-18T19:43:16.231524Z"
    },
    "origin_pos": 18,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 4])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conv2d = nn.LazyConv2d(1, kernel_size=3, padding=1, stride=2)\n",
    "comp_conv2d(conv2d, X).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "751905e3",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "A slightly more complicated example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "530a0750",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:43:16.235915Z",
     "iopub.status.busy": "2023-08-18T19:43:16.235234Z",
     "iopub.status.idle": "2023-08-18T19:43:16.243134Z",
     "shell.execute_reply": "2023-08-18T19:43:16.242281Z"
    },
    "origin_pos": 23,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 2])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conv2d = nn.LazyConv2d(1, kernel_size=(3, 5), padding=(0, 1), stride=(3, 4))\n",
    "comp_conv2d(conv2d, X).shape"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "required_libs": [],
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}